package com.dl.exam.util.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author DL
 * @create 2022-03-19 11:16
 */
@Data
public class Result {

    private Boolean state;

    private Integer code;

    private String message;

    private Map<String, Object> data = new HashMap<String, Object>();

    // 把构造方法私有，不让外部new
    private Result() {
    }

    /**
     * 返回简单成功信息
     * @return 统一返回格式
     */
    public static Result ok() {
        Result result = new Result();
        result.setState(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 返回简单成功信息
     * @return 统一返回格式
     */
    public static Result ok(String message) {
        Result result = new Result();
        result.setState(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 返回简单成功信息
     * @return 统一返回格式
     */
    public static Result no() {
        Result result = new Result();
        result.setState(false);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 返回简单成功信息
     * @return 统一返回格式
     */
    public static Result no(String message) {
        Result result = new Result();
        result.setState(false);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 返回自定义成功信息
     * @return 统一返回格式
     */
    public static Result custom() {
        return new Result();
    }

    /**
     * 返回自定义成功信息
     * @param code 状态码
     * @param message 消息
     * @return 统一返回格式
     */
    public static Result success(Integer code, String message) {
        Result result = new Result();
        result.setState(true);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 返回内置的成功信息
     * @param resultCodeEnum 自定义的返回状态
     * @return 统一返回格式
     */
    public static Result success(ResultCodeEnum resultCodeEnum) {
        Result result = new Result();
        result.setState(true);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }


    /**
     * 返回自定义失败信息
     * @param message 消息
     * @return 统一返回格式
     */
    public static Result error(String message) {
        Result result = new Result();
        result.setState(false);
        result.setCode(500);
        result.setMessage(message);
        return result;
    }

    /**
     * 返回自定义失败信息
     * @param code 状态码
     * @param message 消息
     * @return 统一返回格式
     */
    public static Result error(Integer code, String message) {
        Result result = new Result();
        result.setState(false);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 返回内置的失败信息
     * @param resultCodeEnum 自定义的返回状态
     * @return 统一返回格式
     */
    public static Result error(ResultCodeEnum resultCodeEnum) {
        Result result = new Result();
        result.setState(false);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public Result state(Boolean state) {
        this.setState(state);
        return this;
    }

    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public Result data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }


}

